#include "JDNNPlayer.h"
#include "Games/NiuNiuGameLogic.h"
#include "JDNNSoundFun.h"
#include "Games/NiuNiuCardSprite.h"

void JDNNPlayer::setHandCard(BYTE* pCardData,int iCout)
{
	memcpy(m_kHandCardData,pCardData,sizeof(m_kHandCardData));
	m_kHandCardCout = iCout;
}

cocos2d::Node* JDNNPlayer::getTouchCardNode(cocos2d::Vec2 kTouchPos)
{
	for (int i = 0;i<m_handPanel->getChildrenCount();i++)
	{
		CCardSprite* pSprite = dynamic_cast<CCardSprite*>(m_handPanel->getChildren().at(i));
		if (pSprite == NULL || pSprite->getTag() != 1)
		{
			continue;
		}
		if (utility::IsSpriteInTouch(kTouchPos,pSprite))
		{
			return pSprite;
		}
	}
	return NULL;
}

BYTE JDNNPlayer::getTouchCardVlaue(cocos2d::Node* pNode)
{
	//int iIdex = 0;
	for (int i = 0; i<m_handPanel->getChildrenCount(); i++)
	{
		CCardSprite* pSprite = dynamic_cast<CCardSprite*>(m_handPanel->getChildren().at(i));
		if (pSprite == NULL || pSprite->getTag() != 1)
		{
			continue;
		}
		if (pSprite == pNode)
		{
			return pSprite->getCardValue();//m_kHandCardData[iIdex];
		}
		//iIdex++;
	}
	return NULL;
}

void JDNNPlayer::showCard()
{
	showCard(m_kHandCardData, MAX_COUNT);
}
void JDNNPlayer::showCard(BYTE* cbCardData,BYTE cbCardCount)
{
	m_handPanel->removeAllChildrenWithCleanup(true);
	for (int i = 0; i < cbCardCount; ++i)
	{
		CCardSprite* pSprite = CCardSprite::create(cbCardData[i], m_iIdex, 1);
		if (pSprite)
		{
			int offset = i >= 3 ? 20 : 0;
			if (m_iIdex == 0)
			{
				pSprite->setPosition(m_handPanel->getChildrenCount() * pSprite->getContentSize().width + offset, 0);
			}
			else
			{
				pSprite->setScale(0.5f);
				pSprite->setPosition(m_handPanel->getChildrenCount() * pSprite->getContentSize().width / 5 + offset, 0);
			}
			m_handPanel->addChild(pSprite);
		}
	}
}

void JDNNPlayer::showHandCard()
{
	showHandCard(m_kHandCardData, MAX_COUNT);
}

void JDNNPlayer::showHandCard(BYTE* cbCardData, BYTE cbCardCount)
{
	m_handPanel->removeAllChildrenWithCleanup(true);
	for (int i = 0; i < cbCardCount; ++i)
	{
		CCardSprite* pSprite = CCardSprite::create(cbCardData[i], m_iIdex, 0);
		if (pSprite)
		{
			if (m_iIdex == 0)
			{
				pSprite->setTag(1);
				pSprite->setPosition(m_handPanel->getChildrenCount() * pSprite->getContentSize().width, 0);
			}
			else
			{
				pSprite->setScale(0.5f);
				pSprite->setPosition(m_handPanel->getChildrenCount() * pSprite->getContentSize().width/5, 0);
			}
			m_handPanel->addChild(pSprite);
		}
	}
}

void JDNNPlayer::showInitHandCard()
{
	showInitHandCard(m_kHandCardData, MAX_COUNT);
}

void JDNNPlayer::showInitHandCard(BYTE* cbCardData, BYTE cbCardCount)
{
	m_handPanel->removeAllChildrenWithCleanup(true);
	for (int i = 0; i < cbCardCount; ++i)
	{
		CCardSprite* pSprite = CCardSprite::create(i > 2 ? cbCardData[i] : 99, m_iIdex, 0);
		if (pSprite)
		{
			if (m_iIdex == 0)
			{
				pSprite->setTag(1);
				pSprite->setPosition(m_handPanel->getChildrenCount() * pSprite->getContentSize().width, 0);
			}
			else
			{
				pSprite->setScale(0.5f);
				pSprite->setPosition(m_handPanel->getChildrenCount() * pSprite->getContentSize().width / 5, 0);
			}
			m_handPanel->addChild(pSprite);
		}
	}
}